#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   specification.py
@Time    :   2022/11/24 23:25:09
@Author  :   youle
@Email   :   a_fly0505@163.com
@Copyright : 侵权必究
'''
import pandas as pd
import numpy as np
import logging
import talib
import time
from core.config import Config

class Indicators:
    def __init__(self, config:Config) -> None:
        self.config = config
        # matype :0 = SMA, 1=EMA,2=WMA,3=DEMA,4=TEMA,5=TRIMA,6=KAMA,7=MAMA,8=T3

    def KDJ(self, df:pd.DataFrame, fastk: int = 9, slowd: int = 3, slowj: int = 3):
        if df.values.size <= self.config.recoderLimit:
            logging.WARN("kdj 计算数据大小不满足: {}".format(df.values.size))
            return False, []
        df.columns = ["id", "date", "open", "close", "high", "low"]
        new_df = df.sort_values("date", ascending=True)
        rslowk, rslowd = talib.STOCH(
            new_df['high'].values,
            new_df['low'].values,
            new_df['close'].values,
            fastk_period=fastk,
            slowk_period=2*slowd-1,
            slowk_matype=1,
            slowd_period=2*slowj-1,
            slowd_matype=1)
        print("kdj", rslowk, rslowd)

    def RSI(self, df: pd.DataFrame, threshold: float = 30.0, timeperiod: int = 14, last: int = 1):
        """
        @description: 
        @param: df.columns = ["id", "date", "open", "close", "high", "low"]
        @Returns: 
        """
        
        try:
            if df.values.size <= self.config.recoderLimit:
                # print("======", code, sname, df.values.size)
                return False, []

            df.columns = ["id", "date", "open", "close", "high", "low"]
            new_df = df.sort_values("date", ascending=True)
            close_col = new_df.iloc[:, 3]
            close_val = close_col.values
            rsi = talib.RSI(close_val, timeperiod)
            rsi20 = rsi[-last:]
            # print("rsi ", rsi20)
            for item in rsi20:
                if item <= threshold:
                    return True, rsi
            return False, rsi
        except Exception as e:
            print('catch exception：{}'.format(e))
            return False, []
